---@class RedRob.Minerals : RedRob.Pawn
---@field New fun():RedRob.Minerals
local Minerals = RedRob.CreateClass("Minerals","Pawn")
function Minerals:OnCreate()
    self.modelId = 0
    self.modelName = ""
    self.modelObj = nil
    self.loadRequestId = 0
    ---@type Spine.Unity.SkeletonAnimation
    self.animation = nil
    ---@type UnityEngine.Renderer[]
    self.renders = nil
    self.isLoading = false
    self.isLoaded = false
    self.status = RedRob.Config.MineralStatus.full
    if not self.followData then
        self.followData = {
            isStart = false,target = nil,maxTime = 0,curTime = 0,startPos = Vector3(0,0,0),
            pos = Vector3(0,0,0),endPos = Vector3(0,0,0)
        }
    else
        self.followData.isStart = false
    end
    self.needPlaySound = false
end
function Minerals:GetModelId()
    return self.modelId
end
function Minerals:SetModelId(modelId)
    self.modelId = modelId
    self.cfg =  dataTable.getDataTableByStringId("t_mineral",self.modelId)
    if not self.cfg then
        logError("找不到t_mineral "..self.modelId)
        return
    end
    if self:IsBuff() then
        local buffs = self.cfg.f_buffs
        self:AddBuff(buffs[1],1)
    end
end
function Minerals:OnDestroy()
    if self.needPlaySound and RedRob.KeyController.GetCtrlId() == self.followData.target:GetId() then
        local bType = self:GetBuffType()
        if bType == RedRob.Config.BuffType.atk then
            AudioManager.Play(2031)
        elseif bType == RedRob.Config.BuffType.hp then
            AudioManager.Play(2032)
        elseif bType == RedRob.Config.BuffType.speed then
            AudioManager.Play(2033)
        end
    end
    utimer.remove(self.timerAnimId)
    RedRob.PoolMgr.CancelLoad(self.loadRequestId)
    RedRob.PoolMgr.ReturnPrefabToPool(self.modelName,self.modelObj)
    self.modelObj = nil
    self.loadRequestId = 0
    if self.trailEffect and self.cfg then
        self.trailEffect:SetDuring(tonumber(self.cfg.f_DelayEndTime))
        self.trailEffect:SetAutoDestroy(true)
    end
    self.trailEffect = nil
    self.cfg = nil
end
function Minerals:LoadModel()
    if self.isLoading then
        return
    end
    local cfg = self.cfg
    if not cfg then
        return
    end
    local name = cfg.f_ModelPath
    if self.modelName == name then
        return
    end
    self.isLoading = true
    self.modelName = name
    self.loadRequestId = RedRob.PoolMgr.LoadPrefabFromPool(self.modelName,self.LoadCallback,self)
end
function Minerals:LoadCallback(go)
    self.modelObj = go--newObject(go)
    self.modelObj.transform:SetParent(self.transform,false)
    Util.Lua_SetLocalPositionXYZ(self.modelObj,0,0,0)
    self.animation = self.modelObj:GetComponentInChildren(TSkeletonAnimation)
    self.renders = self.gameObject:GetComponentsInChildren(typeof(UnityEngine.Renderer))
    self:InitObject()
end
---@private
function Minerals:InitObject()
    self.gameObject.name = "m"..self.id
    Util.Lua_SetLocalPositionXYZ(self.transform,self.pos.x,self.pos.y,self.pos.z)
    Util.Lua_SetLocalScaleXYZ(self.transform,1,1,1)
    if self.animation then
        Util.Lua_SetLocalRotationXYZ(self.animation.transform,0,0,0)
    end
    self.boxCollider.size = Vector3(1.5,1.5,0.01)
    self.boxCollider.center = Vector3(0,0,0)
    FireEvent(RedRob.Config.Event.ActorPosChanged,self)
    if self.cfg.f_Type == RedRob.Config.MineralType.box then
        self.animation.skeleton:SetSkin(self.cfg.f_SkinName)
    end
    self:SetStatus(self.status)
end
function Minerals:Update(dt)
    local data = self.followData
    if data.isStart then
        data.endPos:SetV(data.target:GetPos())
        data.endPos.y = data.endPos.y + data.target:GetHeight()/2
        data.curTime = data.curTime + dt
        local t = data.curTime/data.maxTime
        Vector3.Lerp2(data.startPos,data.endPos,t,data.pos)
        self:SetPos(data.pos.x,data.pos.y)
        if t >= 1 then
            data.isStart = false
            self:SetActive(false)
            if self.cfg then
                self.trailEffect:SetDuring(tonumber(self.cfg.f_DelayEndTime))
            end
            self.trailEffect:SetAutoDestroy(true)
        end
    end
end
function Minerals:SetFollow(target,time)
    self.followData.isStart = true
    self.followData.target = target
    self.followData.maxTime = time
    self.followData.curTime = 0
    self.followData.startPos:SetV(self:GetPos())
end

---@param player RedRob.Player
function Minerals:PickUp(player)
    self:SetStatus(RedRob.Config.MineralStatus.digEmpty)
    if self:GetMineralType() == RedRob.Config.MineralType.buff then
        local cfg = self.cfg
        if not cfg then
            return
        end
        self.needPlaySound = true
        self:SetFollow(player,cfg.f_Time/1000)
        if not string.IsNilOrEmpty(cfg.f_ParticleName) then
            local res = "Fx/"..cfg.f_ParticleName
            self.trailEffect = RedRob.EffectMgr.CreateEffect(self:GetId(),res,"",0,0,nil,self:GetSortingOrder(),self:GetId())
            local col = string.split2number(cfg.f_ParticleColor,",")
            self.trailEffect:SetTrailColor(col[1],col[2],col[3],col[4])
        end
    else
        if RedRob.KeyController.GetCtrlId() == player:GetId() then
            AudioManager.Play(2030)
        end
    end
    self:SetDead()
end
function Minerals:SetStatus(status)
    self.status = status
    self:RefreshAnimState()

end
function Minerals:RefreshAnimState()
    if not self.animation then return end
    local Status = RedRob.Config.MineralStatus
    utimer.remove(self.timerAnimId)
    self.timerAnimId = nil
    if self.status == Status.full then
        local time = Util.PlayAction(self.animation,"idle3")
        self.timerAnimId = utimer.delayCall(self.IdleState,time,self)
    elseif self.status == Status.digEmpty then
        Util.PlayAction(self.animation,"idle5",false)
    end
end
function Minerals:IdleState()
    if not self.animation then return end
    self.timerAnimId = nil
    Util.PlayAction(self.animation,"idle4")
end
--被打开
function Minerals:IsOpened()
    return self.status == RedRob.Config.MineralStatus.digEmpty
end
function Minerals:SetSortingOrder(order)
    self.sortingOrder = order
    if not self.renders then return end
    if self:IsDead() then return end
    if not RedRob.CameraController.IsPlayerInScreen(self) then
        return
    end
    local length = self.renders.Length
    for i = 0 , length - 1 do
        self.renders[i].sortingOrder = order * self.orderStep + i
    end
end
function Minerals:GetMineralType()
    return self.cfg.f_Type
end
function Minerals:IsBuff()
    return self:GetMineralType() == RedRob.Config.MineralType.buff
end
function Minerals:GetBuffType()
    if not self:IsBuff() then return 0 end
    local buffList = self:GetBuffList()
    local buff
    for k,v in pairs(buffList) do
        buff = v
        break
    end
    if not buff then return 0 end
    local cfg = dataTable.getDataTableByStringId("t_buff",buff.buffId)
    if not cfg then return 0 end
    local attr = cfg.f_Atribute[1]
    local id = attr[1]
    if id == EnumConst.FighterAttributeEnum.ATK_PER then
        return RedRob.Config.BuffType.atk
    elseif id == EnumConst.FighterAttributeEnum.HP_PER then
        return RedRob.Config.BuffType.hp
    else
        return RedRob.Config.BuffType.speed
    end
end